1   /*                        __    __  __  __    __  ___
2    *                       \  \  /  /    \  \  /  /  __/
3    *                        \  \/  /  /\  \  \/  /  /
4    *                         \____/__/  \__\____/__/.ɪᴏ
5    * ᶜᵒᵖʸʳᶦᵍʰᵗ ᵇʸ ᵛᵃᵛʳ ⁻ ˡᶦᶜᵉⁿˢᵉᵈ ᵘⁿᵈᵉʳ ᵗʰᵉ ᵃᵖᵃᶜʰᵉ ˡᶦᶜᵉⁿˢᵉ ᵛᵉʳˢᶦᵒⁿ ᵗʷᵒ ᵈᵒᵗ ᶻᵉʳᵒ
6    */
7   package io.vavr.collection;
8   
9   import io.vavr.Serializables;
10  import io.vavr.Value;
11  import io.vavr.Tuple2;
12  import io.vavr.control.Option;
13  import org.junit.Test;
14  
15  import java.io.InvalidObjectException;
16  import java.math.BigDecimal;
17  import java.util.ArrayList;
18  import java.util.function.Function;
19  import java.util.function.Supplier;
20  import java.util.stream.Collector;
21  
22  public class VectorTest extends AbstractIndexedSeqTest {
23  
24      @Override
25      protected <T> Collector<T, ArrayList<T>, Vector<T>> collector() {
26          return Vector.collector();
27      }
28  
29      @Override
30      protected <T> Vector<T> empty() {
31          return Vector.empty();
32      }
33  
34      @Override
35      protected <T> Vector<T> of(T element) {
36          return Vector.of(element);
37      }
38  
39      @SuppressWarnings("varargs")
40      @SafeVarargs
41      @Override
42      protected final <T> Vector<T> of(T... elements) {
43          return Vector.of(elements);
44      }
45  
46      @Override
47      protected <T> Vector<T> ofAll(Iterable<? extends T> elements) {
48          return Vector.ofAll(elements);
49      }
50  
51      @Override
52      protected <T extends Comparable<? super T>> Vector<T> ofJavaStream(java.util.stream.Stream<? extends T> javaStream) {
53          return Vector.ofAll(javaStream);
54      }
55  
56      @Override
57      protected Vector<Boolean> ofAll(boolean... elements) {
58          return Vector.ofAll(elements);
59      }
60  
61      @Override
62      protected Vector<Byte> ofAll(byte... elements) {
63          return Vector.ofAll(elements);
64      }
65  
66      @Override
67      protected Vector<Character> ofAll(char... elements) {
68          return Vector.ofAll(elements);
69      }
70  
71      @Override
72      protected Vector<Double> ofAll(double... elements) {
73          return Vector.ofAll(elements);
74      }
75  
76      @Override
77      protected Vector<Float> ofAll(float... elements) {
78          return Vector.ofAll(elements);
79      }
80  
81      @Override
82      protected Vector<Integer> ofAll(int... elements) {
83          return Vector.ofAll(elements);
84      }
85  
86      @Override
87      protected Vector<Long> ofAll(long... elements) {
88          return Vector.ofAll(elements);
89      }
90  
91      @Override
92      protected Vector<Short> ofAll(short... elements) {
93          return Vector.ofAll(elements);
94      }
95  
96      @Override
97      protected <T> Vector<T> tabulate(int n, Function<? super Integer, ? extends T> f) {
98          return Vector.tabulate(n, f);
99      }
100 
101     @Override
102     protected <T> Vector<T> fill(int n, Supplier<? extends T> s) {
103         return Vector.fill(n, s);
104     }
105 
106     @Override
107     protected Vector<Character> range(char from, char toExclusive) {
108         return Vector.range(from, toExclusive);
109     }
110 
111     @Override
112     protected Vector<Character> rangeBy(char from, char toExclusive, int step) {
113         return Vector.rangeBy(from, toExclusive, step);
114     }
115 
116     @Override
117     protected Vector<Double> rangeBy(double from, double toExclusive, double step) {
118         return Vector.rangeBy(from, toExclusive, step);
119     }
120 
121     @Override
122     protected int getPeekNonNilPerformingAnAction() {
123         return 1;
124     }
125 
126     @Override
127     protected boolean useIsEqualToInsteadOfIsSameAs() {
128         return false;
129     }
130 
131     @Override
132     protected Vector<Integer> range(int from, int toExclusive) {
133         return Vector.range(from, toExclusive);
134     }
135 
136     @Override
137     protected Vector<Integer> rangeBy(int from, int toExclusive, int step) {
138         return Vector.rangeBy(from, toExclusive, step);
139     }
140 
141     @Override
142     protected Vector<Long> range(long from, long toExclusive) {
143         return Vector.range(from, toExclusive);
144     }
145 
146     @Override
147     protected Vector<Long> rangeBy(long from, long toExclusive, long step) {
148         return Vector.rangeBy(from, toExclusive, step);
149     }
150 
151     @Override
152     protected Vector<Character> rangeClosed(char from, char toInclusive) {
153         return Vector.rangeClosed(from, toInclusive);
154     }
155 
156     @Override
157     protected Vector<Character> rangeClosedBy(char from, char toInclusive, int step) {
158         return Vector.rangeClosedBy(from, toInclusive, step);
159     }
160 
161     @Override
162     protected Vector<Double> rangeClosedBy(double from, double toInclusive, double step) {
163         return Vector.rangeClosedBy(from, toInclusive, step);
164     }
165 
166     @Override
167     protected Vector<Integer> rangeClosed(int from, int toInclusive) {
168         return Vector.rangeClosed(from, toInclusive);
169     }
170 
171     @Override
172     protected Vector<Integer> rangeClosedBy(int from, int toInclusive, int step) {
173         return Vector.rangeClosedBy(from, toInclusive, step);
174     }
175 
176     @Override
177     protected Vector<Long> rangeClosed(long from, long toInclusive) {
178         return Vector.rangeClosed(from, toInclusive);
179     }
180 
181     @Override
182     protected Vector<Long> rangeClosedBy(long from, long toInclusive, long step) {
183         return Vector.rangeClosedBy(from, toInclusive, step);
184     }
185 
186     @Override
187     @SuppressWarnings("unchecked")
188     protected <T> Vector<Vector<T>> transpose(Seq<? extends Seq<T>> rows) {
189         return Vector.transpose((Vector<Vector<T>>) rows);
190     }
191 
192     // -- static narrow
193 
194     @Test
195     public void shouldNarrowVector() {
196         final Vector<Double> doubles = of(1.0d);
197         final Vector<Number> numbers = Vector.narrow(doubles);
198         final int actual = numbers.append(new BigDecimal("2.0")).sum().intValue();
199         assertThat(actual).isEqualTo(3);
200     }
201 
202     // -- primitives
203 
204     @Test
205     public void shouldAddNullToPrimitiveVector() {
206         final Vector<Integer> primitives = rangeClosed(0, 2);
207 
208         assertThat(primitives.append(null)).isEqualTo(of(0, 1, 2, null));
209         assertThat(primitives.prepend(null)).isEqualTo(of(null, 0, 1, 2));
210         assertThat(primitives.update(1, (Integer) null)).isEqualTo(of(0, null, 2));
211     }
212 
213     @Test
214     public void shouldAddObjectToPrimitiveVector() {
215         final String object = "String";
216         final Vector<Object> primitives = Vector.narrow(rangeClosed(0, 2));
217 
218         assertThat(primitives.append(object)).isEqualTo(of(0, 1, 2, object));
219         assertThat(primitives.prepend(object)).isEqualTo(of(object, 0, 1, 2));
220         assertThat(primitives.update(1, object)).isEqualTo(of(0, object, 2));
221     }
222 
223     @Test(expected = IllegalArgumentException.class)
224     public void shouldThrowForVoidType() {
225         ArrayType.of(void.class);
226     }
227 
228     // -- transform()
229 
230     @Test
231     public void shouldTransform() {
232         final String transformed = of(42).transform(v -> String.valueOf(v.get()));
233         assertThat(transformed).isEqualTo("42");
234     }
235 
236     // -- unfold
237 
238     @Test
239     public void shouldUnfoldRightToEmpty() {
240         assertThat(Vector.unfoldRight(0, x -> Option.none())).isEqualTo(empty());
241     }
242 
243     @Test
244     public void shouldUnfoldRightSimpleVector() {
245         assertThat(
246                 Vector.unfoldRight(10, x -> x == 0
247                                             ? Option.none()
248                                             : Option.of(new Tuple2<>(x, x - 1))))
249                 .isEqualTo(of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
250     }
251 
252     @Test
253     public void shouldUnfoldLeftToEmpty() {
254         assertThat(Vector.unfoldLeft(0, x -> Option.none())).isEqualTo(empty());
255     }
256 
257     @Test
258     public void shouldUnfoldLeftSimpleVector() {
259         assertThat(
260                 Vector.unfoldLeft(10, x -> x == 0
261                                            ? Option.none()
262                                            : Option.of(new Tuple2<>(x - 1, x))))
263                 .isEqualTo(of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
264     }
265 
266     @Test
267     public void shouldUnfoldToEmpty() {
268         assertThat(Vector.unfold(0, x -> Option.none())).isEqualTo(empty());
269     }
270 
271     @Test
272     public void shouldUnfoldSimpleVector() {
273         assertThat(
274                 Vector.unfold(10, x -> x == 0
275                                        ? Option.none()
276                                        : Option.of(new Tuple2<>(x - 1, x))))
277                 .isEqualTo(of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
278     }
279 
280     // -- dropRightWhile
281 
282     @Test
283     public void shouldDropRightWhileNoneOnNil() {
284         assertThat(empty().dropRightWhile(ignored -> true)).isEqualTo(empty());
285     }
286 
287     @Test
288     public void shouldDropRightWhileNoneIfPredicateIsFalse() {
289         assertThat(of(1, 2, 3).dropRightWhile(ignored -> false)).isEqualTo(of(1, 2, 3));
290     }
291 
292     @Test
293     public void shouldDropRightWhileAllIfPredicateIsTrue() {
294         assertThat(of(1, 2, 3).dropRightWhile(ignored -> true)).isEqualTo(empty());
295     }
296 
297     @Test
298     public void shouldDropRightWhileCorrect() {
299         assertThat(ofAll("abc  ".toCharArray()).dropRightWhile(Character::isWhitespace)).isEqualTo(ofAll("abc".toCharArray()));
300     }
301 
302     // -- toString
303 
304     @Test
305     public void shouldStringifyNil() {
306         assertThat(empty().toString()).isEqualTo("Vector()");
307     }
308 
309     @Test
310     public void shouldStringifyNonNil() {
311         assertThat(of(null, 1, 2, 3).toString()).isEqualTo("Vector(null, 1, 2, 3)");
312     }
313 
314     // -- Cons test
315 
316     @Test(expected = InvalidObjectException.class)
317     public void shouldNotSerializeEnclosingClass() throws Throwable {
318         Serializables.callReadObject(List.of(1));
319     }
320 
321     // -- toVector
322 
323     @Test
324     public void shouldReturnSelfOnConvertToVector() {
325         final Value<Integer> value = of(1, 2, 3);
326         assertThat(value.toVector()).isSameAs(value);
327     }
328 }